package com.windersgroup.querynumber.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.windersgroup.querynumber.entity.MachineNum;
import com.windersgroup.querynumber.entity.MachineNumberRecord;
import com.windersgroup.querynumber.entity.UserIpMapping;
import com.windersgroup.querynumber.service.MachineNumberService;
import com.windersgroup.querynumber.service.UserIpMappingService;

@Controller
@RequestMapping("/machine")
public class QueryNumberController {

	@Autowired
	@Qualifier("machineNumberService")
	private MachineNumberService machineNumberService;
	
	@Autowired
	@Qualifier("userIpMappingService")
	private UserIpMappingService userIpMappingService;
	
	@RequestMapping("/error")
	public String getNumberForFirst(HttpServletResponse res,HttpServletRequest req) {
		return "error";
	}
	
	@RequestMapping("/index")
	public String getInitHome(Model model,HttpServletRequest req) {
		String dateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
		Integer maxNum = machineNumberService.getMaxNumToday(dateStr);
		List<MachineNumberRecord> machineNums = machineNumberService.findAll(dateStr);
		List<UserIpMapping> userList = userIpMappingService.findUserByIp(req.getRemoteAddr());
		String userName = "";
		if (!CollectionUtils.isEmpty(userList)) {
			userName = userList.get(0).getUserName();
		}
		model.addAttribute("noid", maxNum);
		model.addAttribute("machineNums", machineNums);
		if (!StringUtils.isEmpty(req.getParameter("isrepeat"))) {
			model.addAttribute("isfirst", "2");
		} else {
			model.addAttribute("isfirst", "1");
		}
		
		model.addAttribute("username", userName);
		return "index";
	}
	
	@RequestMapping("/index/nologin")
	public String getInitHomeWithNoUser(Model model,HttpServletRequest req) {
		String dateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
		Integer maxNum = machineNumberService.getMaxNumToday(dateStr);
		List<MachineNumberRecord> machineNums = machineNumberService.findAll(dateStr);
		model.addAttribute("noid", maxNum);
		model.addAttribute("machineNums", machineNums);
		
		model.addAttribute("isfirst", "3");
		
		model.addAttribute("username", "");
		return "index";
	}
	
	@RequestMapping("/mapData")
	@ResponseBody
	public String getDataForMap() {
		List<MachineNumberRecord> list = machineNumberService.findDataForMap();
		List<UserIpMapping> userList =userIpMappingService.findAll();
		Map<String,Object> userDataMap = new LinkedHashMap<>();
		Map<String,Object> dataMap = new LinkedHashMap<>();
		Map<String,Object> userInfoMap = new LinkedHashMap<>();
		
		dataMap.put("total", list);
		dataMap.put("details", userDataMap);
		dataMap.put("userInfo", userInfoMap);
		if (!CollectionUtils.isEmpty(list)) {
			for (MachineNumberRecord rec : list) {
				List<Map<String,String>> dayList = new ArrayList<>();
				for (UserIpMapping user : userList) {
					Map<String,String> userMap = new LinkedHashMap<>();
					List<MachineNumberRecord> dataList = machineNumberService.findDataByUserAndDate(rec.getDateStr(), user.getIpAddr());
					int count = 0;
					if (!CollectionUtils.isEmpty(dataList)) {
						count = dataList.size();
					}
					userMap.put("name", user.getUserName());
					userMap.put("count", String.valueOf(count));
					dayList.add(userMap);
				}
				userDataMap.put("l" + rec.getDateStr(), dayList);
			}
		}
		userInfoMap.put("total", userList.size());
		userInfoMap.put("username", userList);
		Gson gson = new Gson();
		return gson.toJson(dataMap);
	}
	
	@RequestMapping("/getNum")
	public void getMachineNum( Model model,HttpServletRequest req,HttpServletResponse res) {
		String dateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
//		int noid = MachineNum.getInstance(dateStr);
		Integer maxNum = machineNumberService.getMaxNumToday(dateStr);
		int currentNum = 0;
		if (maxNum != null) {
			currentNum = maxNum.intValue() + 1;
		} else {
			currentNum = 1;
		}
		MachineNumberRecord machineNumberRecord = new MachineNumberRecord();
		machineNumberRecord.setTodayid(Long.valueOf(String.valueOf(currentNum)));
		machineNumberRecord.setName(req.getRemoteAddr());
		machineNumberRecord.setDateStr(dateStr);
		machineNumberService.insertNum(machineNumberRecord);
		model.addAttribute("noid", currentNum);
		List<MachineNumberRecord> machineNums = machineNumberService.findAll(dateStr);
		List<UserIpMapping> userList = userIpMappingService.findUserByIp(req.getRemoteAddr());
		String userName = "";
		if (!CollectionUtils.isEmpty(userList)) {
			userName = userList.get(0).getUserName();
		}
		model.addAttribute("machineNums", machineNums);
		model.addAttribute("isfirst", "2");
		model.addAttribute("username", userName);
		try {
			res.sendRedirect("/machine/index?isrepeat=1");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
